package problem110;

//110.平衡二叉树
//https://leetcode.cn/problems/balanced-binary-tree/

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
class Solution {
    public int maxHeight(TreeNode root) {
        if(root == null) return 0;
        int left = maxHeight(root.left);
        int right = maxHeight(root.right);
        if(Math.abs(left-right) > 1 || left == -1 || right == -1) return -1;
        return Math.max(left, right) + 1;
    }
    
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        int len = maxHeight(root);
        if(len == -1) return false;
        return true;
    }
}